#include <iostream>
#include <unistd.h>
#include<ctime>

int gval =100;

// 新线程
void *threadStart(void *args)
{
    //   int x= rand()%5;
    while (true)
    {
        std::cout << "new thread running..." << ",pid" << getpid() 
        <<"gval:"<<gval<< ",&gval"<<&gval<<std::endl;
        sleep(1);

      
        // if(x==0)
        // {
        //     int *p=nullptr;
        //     *p=100;
        // }
    }
}

int main()
{
    srand(time(nullptr));
    pthread_t tid1;
    pthread_create(&tid1, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid2;
    pthread_create(&tid2, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid3;
    pthread_create(&tid3, nullptr, threadStart, (void *)"thread-new");

    // 主线程
    while (true)
    {
        sleep(1);
        std::cout << "main thread running..." << ",pid" << getpid() 
    <<"gval:"<<gval<< ",&gval"<<&gval<< std::endl;
    }

    return 0;
}